import radio_config
import time

def diff_minutes(str_time1, str_time2):
    def tpl(str_time):
        if str_time <> "24:00":
            timeString1 = "03/06/05 " + str_time
        else:
            timeString1 = "03/07/05 00:00" 
        return time.strptime(timeString1, "%m/%d/%y %H:%M")
    return (time.mktime(tpl(str_time2)) - time.mktime(tpl(str_time1))) / 60.0

def get_weekday_index(weekday):
    i = 0
    for t in radio_config.weekdays:
        if t == weekday: return i
        i = i + 1

def get_cmd(cmd, broadcast_, locals_):
    locals_.update(radio_config.__dict__)
    locals_.update(Broadcast(*broadcast_).__dict__)
    return cmd % locals_


class Broadcast(object):
    def __init__(self, broadcastname, weekday, start, end):
        self.broadcastname = broadcastname
        self.duration = int(diff_minutes(start, end) + radio_config.overflow_time)
        if self.duration < 0:
            raise ValueError , "Start '%s' is after the end '%s'" % (start, end)
        self.weekday_index = get_weekday_index(weekday) + 1 # so = 0, mo=1
        if self.weekday_index > 6: self.weekday_index = 0
        self.weekday = weekday
        self.minute = start.split(":")[1]
        self.hour = start.split(":")[0]
